{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nAuto-tuning a Convolutional Network for Mobile GPU\n==================================================\n**Author**: `Lianmin Zheng <https://github.com/merrymercy>`_, `Eddie Yan <https://github.com/eqy>`_\n\nAuto-tuning for a specific device is critical for getting the best\nperformance. This is a tutorial about how to tune a whole convolutional\nnetwork.\n\nThe operator implementation for Mobile GPU in TVM is written in template form.\nThe template has many tunable knobs (tile factor, vectorization, unrolling, etc).\nWe will tune all convolution, depthwise convolution and dense operators\nin the neural network. After tuning, we produce a log file which stores\nthe best knob values for all required operators. When the TVM compiler compiles\nthese operators, it will query this log file to get the best knob values.\n\nWe also released pre-tuned parameters for some arm devices. You can go to\n`Mobile GPU Benchmark <https://github.com/apache/tvm/wiki/Benchmark#mobile-gpu>`_\nto see the results.\n\nNote that this tutorial will not run on Windows or recent versions of macOS. To\nget it to run, you will need to wrap the body of this tutorial in a :code:`if\n__name__ == \"__main__\":` block.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Install dependencies\n--------------------\nTo use the autotvm package in tvm, we need to install some extra dependencies.\n(change \"3\" to \"2\" if you use python2):\n\n.. code-block:: bash\n\n  pip3 install --user psutil xgboost tornado cloudpickle\n\nTo make TVM run faster during tuning, it is recommended to use cython\nas FFI of tvm. In the root directory of tvm, execute\n(change \"3\" to \"2\" if you use python2):\n\n.. code-block:: bash\n\n  pip3 install --user cython\n  sudo make cython3\n\nNow return to python code. Import packages.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\nimport numpy as np\n\nimport tvm\nfrom tvm import relay, autotvm\nimport tvm.relay.testing\nfrom tvm.autotvm.tuner import XGBTuner, GATuner, RandomTuner, GridSearchTuner\nfrom tvm.contrib.utils import tempdir\nimport tvm.contrib.graph_executor as runtime"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define network\n--------------\nFirst we need to define the network in relay frontend API.\nWe can load some pre-defined network from :code:`relay.testing`.\nWe can also load models from MXNet, ONNX and TensorFlow.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def get_network(name, batch_size):\n    \"\"\"Get the symbol definition and random weight of a network\"\"\"\n    input_shape = (batch_size, 3, 224, 224)\n    output_shape = (batch_size, 1000)\n\n    if \"resnet\" in name:\n        n_layer = int(name.split(\"-\")[1])\n        mod, params = relay.testing.resnet.get_workload(\n            num_layers=n_layer, batch_size=batch_size, dtype=dtype\n        )\n    elif \"vgg\" in name:\n        n_layer = int(name.split(\"-\")[1])\n        mod, params = relay.testing.vgg.get_workload(\n            num_layers=n_layer, batch_size=batch_size, dtype=dtype\n        )\n    elif name == \"mobilenet\":\n        mod, params = relay.testing.mobilenet.get_workload(batch_size=batch_size, dtype=dtype)\n    elif name == \"squeezenet_v1.1\":\n        mod, params = relay.testing.squeezenet.get_workload(\n            batch_size=batch_size, version=\"1.1\", dtype=dtype\n        )\n    elif name == \"inception_v3\":\n        input_shape = (batch_size, 3, 299, 299)\n        mod, params = relay.testing.inception_v3.get_workload(batch_size=batch_size, dtype=dtype)\n    elif name == \"mxnet\":\n        # an example for mxnet model\n        from mxnet.gluon.model_zoo.vision import get_model\n\n        block = get_model(\"resnet18_v1\", pretrained=True)\n        mod, params = relay.frontend.from_mxnet(block, shape={\"data\": input_shape}, dtype=dtype)\n        net = mod[\"main\"]\n        net = relay.Function(\n            net.params, relay.nn.softmax(net.body), None, net.type_params, net.attrs\n        )\n        mod = tvm.IRModule.from_expr(net)\n    else:\n        raise ValueError(\"Unsupported network: \" + name)\n\n    return mod, params, input_shape, output_shape"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Start RPC Tracker\n-----------------\nTVM uses RPC session to communicate with ARM boards.\nDuring tuning, the tuner will send the generated code to the board and\nmeasure the speed of code on the board.\n\nTo scale up the tuning, TVM uses RPC Tracker to manage distributed devices.\nThe RPC Tracker is a centralized controller node. We can register all devices to\nthe tracker. For example, if we have 10 phones, we can register all of them\nto the tracker, and run 10 measurements in parallel, accelerating the tuning process.\n\nTo start an RPC tracker, run this command on the host machine. The tracker is\nrequired during the whole tuning process, so we need to open a new terminal for\nthis command:\n\n.. code-block:: bash\n\n  python -m tvm.exec.rpc_tracker --host=0.0.0.0 --port=9190\n\nThe expected output is\n\n.. code-block:: bash\n\n  INFO:RPCTracker:bind to 0.0.0.0:9190\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Register Devices to RPC Tracker\n-----------------------------------\nNow we can register our devices to the tracker. The first step is to\nbuild the TVM runtime for the ARM devices.\n\n* For Linux:\n  Follow this section `build-tvm-runtime-on-device` to build\n  the TVM runtime on the device. Then register the device to tracker by\n\n  .. code-block:: bash\n\n    python -m tvm.exec.rpc_server --tracker=[HOST_IP]:9190 --key=rk3399\n\n  (replace :code:`[HOST_IP]` with the IP address of your host machine)\n\n* For Android:\n  Follow this `readme page <https://github.com/apache/tvm/tree/main/apps/android_rpc>`_ to\n  install TVM RPC APK on the android device. Make sure you can pass the android RPC test.\n  Then you have already registered your device. During tuning, you have to go to developer option\n  and enable \"Keep screen awake during changing\" and charge your phone to make it stable.\n\nAfter registering devices, we can confirm it by querying rpc_tracker\n\n.. code-block:: bash\n\n  python -m tvm.exec.query_rpc_tracker --host=0.0.0.0 --port=9190\n\nFor example, if we have 2 Huawei mate10 pro, 11 Raspberry Pi 3B and 2 rk3399,\nthe output can be\n\n.. code-block:: bash\n\n   Queue Status\n   ----------------------------------\n   key          total  free  pending\n   ----------------------------------\n   mate10pro    2      2     0\n   rk3399       2      2     0\n   rpi3b        11     11    0\n   ----------------------------------\n\nYou can register multiple devices to the tracker to accelerate the measurement in tuning.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set Tuning Options\n------------------\nBefore tuning, we should apply some configurations. Here I use an RK3399 board\nas example. In your setting, you should modify the target and device_key accordingly.\nset :code:`use_android` to True if you use android phone.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "#### DEVICE CONFIG ####\n# Replace \"aarch64-linux-gnu\" with the correct target of your board.\n# This target host is used for cross compilation. You can query it by :code:`gcc -v` on your device.\ntarget = tvm.target.Target(\"opencl -device=mali\", host=\"llvm -mtriple=aarch64-linux-gnu\")\n\n# Also replace this with the device key in your tracker\ndevice_key = \"rk3399\"\n\n# Set this to True if you use android phone\nuse_android = False\n\n#### TUNING OPTION ####\nnetwork = \"resnet-18\"\nlog_file = \"%s.%s.log\" % (device_key, network)\ndtype = \"float32\"\n\ntuning_option = {\n    \"log_filename\": log_file,\n    \"tuner\": \"xgb\",\n    \"n_trial\": 1000,\n    \"early_stopping\": 450,\n    \"measure_option\": autotvm.measure_option(\n        builder=autotvm.LocalBuilder(build_func=\"ndk\" if use_android else \"default\"),\n        runner=autotvm.RPCRunner(\n            device_key,\n            host=\"127.0.0.1\",\n            port=9190,\n            number=10,\n            timeout=5,\n        ),\n    ),\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>How to set tuning options\n\n  In general, the default values provided here work well.\n  If you have enough time budget, you can set :code:`n_trial`, :code:`early_stopping` larger,\n  which makes the tuning run longer.\n  If your device runs very slow or your conv2d operators have many GFLOPs, considering to\n  set timeout larger.</p></div>\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Begin Tuning\n------------\nNow we can extract tuning tasks from the network and begin tuning.\nHere, we provide a simple utility function to tune a list of tasks.\nThis function is just an initial implementation which tunes them in sequential order.\nWe will introduce a more sophisticated tuning scheduler in the future.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# You can skip the implementation of this function for this tutorial.\ndef tune_tasks(\n    tasks,\n    measure_option,\n    tuner=\"xgb\",\n    n_trial=1000,\n    early_stopping=None,\n    log_filename=\"tuning.log\",\n    use_transfer_learning=True,\n):\n    # create tmp log file\n    tmp_log_file = log_filename + \".tmp\"\n    if os.path.exists(tmp_log_file):\n        os.remove(tmp_log_file)\n\n    for i, tsk in enumerate(reversed(tasks)):\n        prefix = \"[Task %2d/%2d] \" % (i + 1, len(tasks))\n\n        # create tuner\n        if tuner == \"xgb\" or tuner == \"xgb-rank\":\n            tuner_obj = XGBTuner(tsk, loss_type=\"rank\")\n        elif tuner == \"ga\":\n            tuner_obj = GATuner(tsk, pop_size=50)\n        elif tuner == \"random\":\n            tuner_obj = RandomTuner(tsk)\n        elif tuner == \"gridsearch\":\n            tuner_obj = GridSearchTuner(tsk)\n        else:\n            raise ValueError(\"Invalid tuner: \" + tuner)\n\n        if use_transfer_learning:\n            if os.path.isfile(tmp_log_file):\n                tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file))\n\n        # do tuning\n        tsk_trial = min(n_trial, len(tsk.config_space))\n        tuner_obj.tune(\n            n_trial=tsk_trial,\n            early_stopping=early_stopping,\n            measure_option=measure_option,\n            callbacks=[\n                autotvm.callback.progress_bar(tsk_trial, prefix=prefix),\n                autotvm.callback.log_to_file(tmp_log_file),\n            ],\n        )\n\n    # pick best records to a cache file\n    autotvm.record.pick_best(tmp_log_file, log_filename)\n    os.remove(tmp_log_file)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, we launch tuning jobs and evaluate the end-to-end performance.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def tune_and_evaluate(tuning_opt):\n    # extract workloads from relay program\n    print(\"Extract tasks...\")\n    mod, params, input_shape, _ = get_network(network, batch_size=1)\n    tasks = autotvm.task.extract_from_program(\n        mod[\"main\"],\n        target=target,\n        params=params,\n        ops=(relay.op.get(\"nn.conv2d\"),),\n    )\n\n    # run tuning tasks\n    print(\"Tuning...\")\n    tune_tasks(tasks, **tuning_opt)\n\n    # compile kernels with history best records\n    with autotvm.apply_history_best(log_file):\n        print(\"Compile...\")\n        with tvm.transform.PassContext(opt_level=3):\n            lib = relay.build_module.build(mod, target=target, params=params)\n        # export library\n        tmp = tempdir()\n        if use_android:\n            from tvm.contrib import ndk\n\n            filename = \"net.so\"\n            lib.export_library(tmp.relpath(filename), ndk.create_shared)\n        else:\n            filename = \"net.tar\"\n            lib.export_library(tmp.relpath(filename))\n\n        # upload module to device\n        print(\"Upload...\")\n        remote = autotvm.measure.request_remote(device_key, \"127.0.0.1\", 9190, timeout=10000)\n        remote.upload(tmp.relpath(filename))\n        rlib = remote.load_module(filename)\n\n        # upload parameters to device\n        dev = remote.device(str(target), 0)\n        module = runtime.GraphModule(rlib[\"default\"](dev))\n        data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))\n        module.set_input(\"data\", data_tvm)\n\n        # evaluate\n        print(\"Evaluate inference time cost...\")\n        print(module.benchmark(dev, number=1, repeat=30))\n\n\n# We do not run the tuning in our webpage server since it takes too long.\n# Uncomment the following line to run it by yourself.\n\n# tune_and_evaluate(tuning_option)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Sample Output\n-------------\nThe tuning needs to compile many programs and extract feature from them.\nSo a high performance CPU is recommended.\nOne sample output is listed below. It takes about 3 hours on a 32T AMD Ryzen Threadripper.\n\n.. code-block:: bash\n\n   Extract tasks...\n   Tuning...\n   [Task  1/17]  Current/Best:   25.30/  39.12 GFLOPS | Progress: (992/1000) | 751.22 s Done.\n   [Task  2/17]  Current/Best:   40.70/  45.50 GFLOPS | Progress: (736/1000) | 545.46 s Done.\n   [Task  3/17]  Current/Best:   38.83/  42.35 GFLOPS | Progress: (992/1000) | 1549.85 s Done.\n   [Task  4/17]  Current/Best:   23.31/  31.02 GFLOPS | Progress: (640/1000) | 1059.31 s Done.\n   [Task  5/17]  Current/Best:    0.06/   2.34 GFLOPS | Progress: (544/1000) | 305.45 s Done.\n   [Task  6/17]  Current/Best:   10.97/  17.20 GFLOPS | Progress: (992/1000) | 1050.00 s Done.\n   [Task  7/17]  Current/Best:    8.98/  10.94 GFLOPS | Progress: (928/1000) | 421.36 s Done.\n   [Task  8/17]  Current/Best:    4.48/  14.86 GFLOPS | Progress: (704/1000) | 582.60 s Done.\n   [Task  9/17]  Current/Best:   10.30/  25.99 GFLOPS | Progress: (864/1000) | 899.85 s Done.\n   [Task 10/17]  Current/Best:   11.73/  12.52 GFLOPS | Progress: (608/1000) | 304.85 s Done.\n   [Task 11/17]  Current/Best:   15.26/  18.68 GFLOPS | Progress: (800/1000) | 747.52 s Done.\n   [Task 12/17]  Current/Best:   17.48/  26.71 GFLOPS | Progress: (1000/1000) | 1166.40 s Done.\n   [Task 13/17]  Current/Best:    0.96/  11.43 GFLOPS | Progress: (960/1000) | 611.65 s Done.\n   [Task 14/17]  Current/Best:   17.88/  20.22 GFLOPS | Progress: (672/1000) | 670.29 s Done.\n   [Task 15/17]  Current/Best:   11.62/  13.98 GFLOPS | Progress: (736/1000) | 449.25 s Done.\n   [Task 16/17]  Current/Best:   19.90/  23.83 GFLOPS | Progress: (608/1000) | 708.64 s Done.\n   [Task 17/17]  Current/Best:   17.98/  22.75 GFLOPS | Progress: (736/1000) | 1122.60 s Done.\n   Compile...\n   Upload...\n   Evaluate inference time cost...\n   Mean inference time (std dev): 128.05 ms (7.74 ms)\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>**Experiencing Difficulties?**\n\n  The auto tuning module is error-prone. If you always see \" 0.00/ 0.00 GFLOPS\",\n  then there must be something wrong.\n\n  First, make sure you set the correct configuration of your device.\n  Then, you can print debug information by adding these lines in the beginning\n  of the script. It will print every measurement result, where you can find useful\n  error messages.\n\n  .. code-block:: python\n\n     import logging\n     logging.getLogger('autotvm').setLevel(logging.DEBUG)\n\n  Finally, always feel free to ask our community for help on https://discuss.tvm.apache.org</p></div>\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}